package com.listeningframework.boot.autoconfigure.webmvc;

import com.listeningframework.boot.commons.string.StringUtils;
import com.listeningframework.boot.commons.utils.CollectionUtils;
import com.listeningframework.boot.cors.CorsProperties;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.*;

import java.util.List;

/**
 * @author listening
 * @version $Id: ListeningWebMvcConfigurerAdapter.java, v 0.1 2015年4月12日
 *          下午4:44:04 listening Exp $
 * Web Mvc Default Controller
 */
@Configuration
@EnableConfigurationProperties(CorsProperties.class)
public class ListeningWebMvcConfigurerAdapter extends WebMvcConfigurerAdapter {

    @Autowired
    private CorsProperties corsProperties;

    @Autowired(required = false)
    ListeningInterceptorAdapter[] interceptorAdapterArray;

    @Override
    public void addViewControllers(ViewControllerRegistry registry) {
        registry.addViewController("/").setViewName("redirect:/main");
    }

    /**
     * @see org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter#addInterceptors(org.springframework.web.servlet.config.annotation.InterceptorRegistry)
     */
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        super.addInterceptors(registry);
        if (interceptorAdapterArray == null || interceptorAdapterArray.length <= 0) {
            return;
        }
        for (ListeningInterceptorAdapter inteceptor : interceptorAdapterArray) {
            registry.addInterceptor(inteceptor).addPathPatterns(inteceptor.getMapping());
        }
    }

    /**
     * global Cors Settings
     */
    @Override
    public void addCorsMappings(CorsRegistry registry) {
        super.addCorsMappings(registry);
        if (corsProperties == null || !corsProperties.isEnabled()) {
            return;
        }
        String allowPattern = corsProperties.getAllowedPattern();
        if (StringUtils.isBlank(allowPattern)) {
            return;
        }
        List<String> mappings = CollectionUtils.tokenizeToList(allowPattern, ",");
        if (CollectionUtils.isEmpty(mappings)) {
            return;
        }
        String allowOrigins = corsProperties.getAllowedOrigins();
        String allowHeaders = corsProperties.getAllowedHttpHeaders();
        String allowMethods = corsProperties.getAllowedHttpMethods();
        String maxAge = corsProperties.getPreflightMaxAge();
        String supportsCredentials = corsProperties.getSupportsCredentials();
        for (String mapping : mappings) {
            CorsRegistration corsRegistration = registry.addMapping(mapping);
            corsRegistration.allowCredentials(Boolean.parseBoolean(supportsCredentials));
            corsRegistration.allowedMethods(getArrayValue(allowMethods));
            corsRegistration.allowedOrigins(getArrayValue(allowOrigins));
            corsRegistration.allowedHeaders(getArrayValue(allowHeaders));
            corsRegistration.maxAge(Long.valueOf(maxAge));
        }
    }

    private String[] getArrayValue(String value) {
        return CollectionUtils.tokenizeToUniqueArray(value);
    }
}